package nl.hu.verbrandendephoenix.controller;

import java.util.ArrayList;

import nl.hu.verbrandendephoenix.client.AppManager;
import nl.hu.verbrandendephoenix.client.NotificationManager;
import nl.hu.verbrandendephoenix.shared.Auction;
import nl.hu.verbrandendephoenix.shared.Bid;
import nl.hu.verbrandendephoenix.shared.FieldVerifier;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

public class SearchAuction {

	private nl.hu.verbrandendephoenix.view.SearchAuction searchAuctionForm;

	public SearchAuction() { 

	}

	public Panel getPanel() {
		searchAuctionForm = new nl.hu.verbrandendephoenix.view.SearchAuction();
		Panel p = searchAuctionForm.constructForm();
		getCategoryList();
		addHandlers();
		return p;
	}

	public Panel getResultPanel(Grid g) {
		searchAuctionForm = new nl.hu.verbrandendephoenix.view.SearchAuction();
		Panel p = searchAuctionForm.constructResultForm(g);
		addResultHandlers();
		return p;
	}

	private void processSubmit() {
		final NotificationManager nM =  NotificationManager.get();

		if(!checkFields()) {
			nM.createNotification("Er is iets mis gegaan!");
		} else {
			nM.createNotification("Zoeken..");

			String prodName = searchAuctionForm.getTxtProductName().getText();
			String minPrice = searchAuctionForm.getTxtPriceMin().getText();
			String maxPrice = searchAuctionForm.getTxtPriceMax().getText();

			AppManager.get().getRpcService().searchAuction(prodName, minPrice, maxPrice, searchAuctionForm.getLboCategorySelection(), new AsyncCallback<ArrayList<Auction>>() {

				public void onFailure(Throwable caught) {
					Window.alert("Communication Failure!");
				}

				public void onSuccess(final ArrayList<Auction> result) {
					if(result.size() != 0) {
						AppManager.get().getRpcService().getAllHighestBids(new AsyncCallback<ArrayList<Bid>>() {

							public void onFailure(Throwable caught) {
								Window.alert(caught.getMessage());
							}

							public void onSuccess(ArrayList<Bid> result2) {
								RootPanel pnlContent = RootPanel.get("content");
								pnlContent.clear();
								pnlContent.add(getResultPanel(searchAuctionForm.printResult(result, result2)));
							}
						});
						
					} 
					else {
						nM.createNotification("Geen veilingen gevonden.");
					}
				}
			});
		}
	}

	private boolean checkFields() {
		FieldVerifier fv = FieldVerifier.get();
		boolean b = true; 

		TextBox txtProductName = searchAuctionForm.getTxtProductName();
		if(txtProductName.getText().equals(""))
			fv.removeRequired(txtProductName);
		else if(!fv.checkString(4, 30, txtProductName.getText())) {
			fv.addRequired(txtProductName);
			b = false;
		} else {
			fv.removeRequired(txtProductName);
		}

		TextBox txtPriceMin = searchAuctionForm.getTxtPriceMin();
		if(txtPriceMin.getText().equals(""))
			fv.removeRequired(txtPriceMin);
		else if(!fv.checkDouble(txtPriceMin.getText().toString())) {
			fv.addRequired(txtPriceMin);
			b = false;
		} else {
			fv.removeRequired(txtPriceMin);
		}

		TextBox txtPriceMax = searchAuctionForm.getTxtPriceMax();
		if(txtPriceMax.getText().equals(""))
			fv.removeRequired(txtPriceMax);
		else if(!fv.checkDouble(txtPriceMax.getText().toString())) {
			fv.addRequired(txtPriceMax);
			b = false;
		} else {
			fv.removeRequired(txtPriceMax);
		}

		return b;
	}

	public void addHandlers() {
		Button btnSearch = searchAuctionForm.getBtnSearch();
		btnSearch.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				processSubmit();
			} 
		});

		ArrayList<TextBox> arrTextBoxes = searchAuctionForm.getArrTextBox();
		for(TextBox tb : arrTextBoxes) {
			tb.addKeyPressHandler(new KeyPressHandler() {
				@Override
				public void onKeyPress(KeyPressEvent event) {
					if(event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER) {
						processSubmit();
					}
				}
			});
		}
	}

	public void addResultHandlers() {
		Button btnBack = searchAuctionForm.getBtnBack();
		btnBack.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				RootPanel pnlContent = RootPanel.get("content");
				pnlContent.clear();
				pnlContent.add(getPanel());
			} 
		});
	}

	public void getCategoryList() {
		AppManager.get().getRpcService().getCategories(new AsyncCallback<String[]>() {
			public void onFailure(Throwable caught) {
				Window.alert("Communication Failure!");
			}

			public void onSuccess(String[] result) {
				searchAuctionForm.setLboCategory(result);
			}
		});
	}
}